package com.jy.novel.dao.system;

import com.google.common.collect.Lists;
import com.jy.novel.common.JdbcHelper;
import com.jy.novel.common.Page;
import com.jy.novel.pojo.form.MenuAddForm;
import com.jy.novel.pojo.form.MenuEditForm;
import com.jy.novel.pojo.system.MenuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jihb on 4/14/16.
 */
@Repository
public class MenuDao {

    @Autowired
    JdbcHelper jdbcHelper;

    /**
     * 查询菜单列表
     * @param parentId
     * @return
     */
    public List<MenuInfo> findMenuList(long parentId,Integer level){
        StringBuilder sb = new StringBuilder();
        sb.append("select id,parentId,name,path,remark,menuId,leaf,creteTime,updateTime from system_menu where parentId=? ");
        List params = Lists.newArrayList(parentId);
        if (level != null){
            sb.append(" and level=? ");
            params.add(level);
        }
        return jdbcHelper.queryForListBean(sb.toString(),params.toArray(),MenuInfo.class);
    }

    /**
     * 查询菜单 分页
     * @param parentId
     * @param curPage
     * @param pageSize
     * @return
     */
    public Page<MenuInfo> findMenuPage(long parentId,Integer level,int curPage,int pageSize){
        StringBuilder sb = new StringBuilder();
        sb.append("select id,parentId,name,path,remark,menuId,leaf,creteTime,updateTime from system_menu where parentId=? ");
        List params = Lists.newArrayList(parentId);
        if (level != null){
            sb.append(" and level=? ");
            params.add(level);
        }
        return jdbcHelper.queryForPageBean(sb.toString(),params.toArray(),MenuInfo.class,curPage,pageSize);
    }

    /**
     * 查询菜单详情
     * @param id
     * @return
     */
    public MenuInfo findMenuInfo(long id){
        StringBuilder sb = new StringBuilder();
        sb.append("select id,parentId,name,path,remark,menuId,leaf,creteTime,updateTime,level from system_menu where id=? ");
        return jdbcHelper.queryForBean(sb.toString(),new Object[]{id},MenuInfo.class);
    }

    /**
     * 添加菜单
     * @param addForm
     * @return
     */
    public boolean addMenuInfo(MenuAddForm addForm){
        StringBuilder sb = new StringBuilder();
        sb.append(" insert into system_menu(parentId,name,path,remark,menuId,leaf,creteTime,updateTime,level) values(?,?,?,?,?,?,?,?,?)");
        List params = new ArrayList();
        params.add(addForm.getParentId());
        params.add(addForm.getName());
        params.add(addForm.getPath());
        params.add(addForm.getRemark());
        params.add(addForm.getMenuId());
        params.add(addForm.getLeaf());
        params.add(addForm.getCreteTime());
        params.add(addForm.getUpdateTime());
        params.add(addForm.getLevel());
        return jdbcHelper.update(sb.toString(),params.toArray()) > 0;
    }

    /**
     * 更新菜单信息
     * @param editForm
     * @return
     */
    public boolean updateMenuInfo(MenuEditForm editForm,long id){
        StringBuilder sb = new StringBuilder();
        sb.append(" update system_menu set updateTime=?");
        List params = new ArrayList();
        params.add(editForm.getUpdateTime());
        if (editForm.getParentId() != null){
            sb.append(",parentId=?");
            params.add(editForm.getParentId());
        }
        if (editForm.getName() != null){
            sb.append(",name=?");
            params.add(editForm.getName());
        }
        if (editForm.getPath() != null){
            sb.append(",path=?");
            params.add(editForm.getPath());
        }
        if (editForm.getRemark() != null){
            sb.append(",remark=?");
            params.add(editForm.getRemark());
        }
        if (editForm.getLeaf() != null){
            sb.append(",leaf=?");
            params.add(editForm.getLeaf());
        }
        sb.append(" where id=? ");
        params.add(id);
        return jdbcHelper.update(sb.toString(),params.toArray()) > 0;
    }

    /**
     * 删除菜单
     * @param id
     * @return
     */
    public boolean deleteMenuInfo(long id){
        return jdbcHelper.update("delete from system_menu where id=? ",new Object[]{id})>0;
    }
}
